package leetco.test.union;

import org.junit.Test;

/**
 * 一个字符串中可能包含元音辅音字母和数字，请写出该字符串反转元音字母之后的结果，提示元音字母共十个a、e、i、o、u、A、E、I、O、U：
 * <p>
 * 输入： “hello2WORld”
 * 输出： “hOllo2WeRld”
 * <p>
 * 输入： “abCd”
 * 输出： “abCd”
 */
public class Reverse {

    private char[] chars = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};


    @Test
    public void tester() {
        System.out.println(changeLetter("hello2WORld"));
        System.out.println(changeLetter("abCd"));
    }

    private String changeLetter(String str) {
        // 存放下标
        int length = str.length();
        int start = 0, end = length - 1;
        int[] index = new int[str.length()];
        char[] chars = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            if (isLetter(str.charAt(i))) {
                index[i] = 1;
            }
        }
        while (start < end) {
            while (index[start] != 1) {
                start++;
            }
            while (index[end] != 1) {
                end--;
            }
            if (start < end) {
                char tmp;
                tmp = chars[start];
                chars[start] = chars[end];
                chars[end] = tmp;
                start++;
                end--;
            }
        }

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < length; i++) {
            result.append(chars[i]);
        }
        return result.toString();
    }


    private boolean isLetter(char c) {
        for (char ch : chars) {
            if (ch == c) {
                return true;
            }
        }
        return false;
    }
}
